<template>
 <div class="publish-container">
  <el-card>
      <div slot="header" class="clearfix">
        <my-breadcrumb>发布文章</my-breadcrumb>
      </div>
       <!-- 表单 -->
       <!-- ref属性为了让vue可以找到这个组 -->
       <!-- 2.绑定表单的rules和表单元素的prop -->
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content" :options="editorOption" />
        </el-form-item>

        <el-form-item label="封面">
          <el-radio-group v-model="form.coverType" @change="hCoverTypeChange">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item v-if="form.coverType>0">
          <el-row>
            <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
              {{idx}}<my-cover v-model="form.coverImages[idx]"/>
            </el-col>
          </el-row>
        </el-form-item>

      <!-- 频道组件 -->
       <my-channel v-model="form.channelId"></my-channel>
       <!-- 按钮 -->
       <!-- hPublish 是验证规则 点击的时候要先触发验证规则如果验证规则不通过就不会调用发布的函数执行
       然后在去调用发布的函数执行 -->
      <el-form-item>
        <el-button
        :loading="isLoading"
        :disabled="isDisabled"
        type="primary" @click="hSave">保存修改</el-button>
      </el-form-item>
    </el-form>
  </el-card>
 </div>
</template>
<script>
// 需要以插件的格式在vue项目中使用，它就是一个普通的组件，直接在你需要用到的地址进行：导入，注册，使用即可。
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'AddArticle',
  components: {
    quillEditor
  },
  data () {
    return {
      isLoading: false, // 发布按钮加载的效果
      isDisabled: false, // 发布按钮禁用的效果
      isLoading1: false, // 草稿按钮加载的效果
      isDisabled1: false, // 草稿按钮加载的效果
      form: {
        title: '', // 标题
        content: '',
        coverType: 0, // 默认是无图
        channelId: '',
        coverImages: [] // 保存用户选中的封面图片的地址
      },
      // 富文本配置对象
      editorOption: {
        // 占位多少配置
        placeholder: '',
        modules: {
        // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      // 1.验证规则配置对象
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      }
    }
  },
  // 一打开就获取的id
  created () {
    this.loadArticle(this.$route.params.id)
  },
  methods: {
    // 在提交数据之前做验证
    hPublish (isDraft) {
      this.$refs.form.validate(valid => {
        //  简便写法 如果前面的值为真就执行后面的
        // valid && this.hAdd(isDraft)
        if (valid) {
          if (isDraft) {
            this.isLoading1 = !this.isLoading1
            this.isDisabled = !this.isDisabled
          } else {
            this.isLoading = !this.isLoading
            this.isDisabled1 = !this.isDisabled1
          }
          this.hAdd(isDraft)
        } else {
          return false
        }
      })
    },
    // 拼接参数
    buildParam () {
    // 解构赋值
      const { title, content, channelId, coverType, coverImages } = this.form
      const paramsObj = {
        title: title,
        content: content,
        cover: {
          type: coverType,
          images: coverImages // 封面图片的地址
        },
        channel_id: channelId
      }
      return paramsObj
    },
    //  发请求,获取文章详情数据回填
    async loadArticle (id) {
      try {
        const res = await this.$axios({
          method: 'GET',
          url: '/mp/v1_0/articles/' + id
        })
        console.log(res)
        const { title, channel_id: channelId, content, cover } = res.data.data
        this.form.id = id
        this.form.title = title
        this.form.channelId = channelId
        this.form.content = content
        this.form.coverType = cover.type
        this.form.coverImages = cover.images
      } catch (err) {
        console.log(err)
      }
    },
    // 发布文章功能
    async hAdd (isDraft) {
      const paramsObj = this.buildParam()
      try {
        const res = await this.$axios({
          method: 'POST',
          url: '/mp/v1_0/articles?draft=' + isDraft,
          data: paramsObj
        })
        console.log(res)
        this.$message.success('添加文章成功')
      } catch (err) {
        this.$message.error('添加文章失败')
        console.log(err)
      }
      if (isDraft) {
        this.isLoading1 = !this.isLoading1
        this.isDisabled = !this.isDisabled
      } else {
        this.isLoading = !this.isLoading
        this.isDisabled1 = !this.isDisabled1
      }
    },
    //  在不同的封面类型上切换
    hCoverTypeChange () {
      if (this.form.coverType === 1) {
        // 数组:arr.length =1;只保存arr[0]的值其他的会被清空掉
        this.form.coverImages.length = 1
      } else if (this.form.coverType <= 0) {
        this.form.coverImages.length = 0
      }
    }
  }
}
</script>

<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
